﻿using System;
using System.ComponentModel;
using System.Text;

namespace SenetExperimentsRunner.Utils
{
    public class GeneralSubPropertyDescriptor : PropertyDescriptor
    {
        private PropertyDescriptor _subPD;
        private PropertyDescriptor _parentPD;
        private string[] _categories;

        public GeneralSubPropertyDescriptor(
                     PropertyDescriptor parentPD,
                     PropertyDescriptor subPD,
                     string pdname)
            : base(pdname, null)
        {
            _subPD = subPD;
            _parentPD = parentPD;
            _categories = pdname.Split('.');
        }

        public override string Category
        {
            get
            {
                string retValue = null;
                if (_categories != null &&
                    _categories.Length >= 2)
                {
                    if (_categories.Length == 2)
                    {
                        retValue = _categories[0];
                    }
                    else
                    {
                        retValue = _categories[1];
                    }
                }
                else
                {
                    retValue = base.Category;
                }
                return retValue;
            }
        }

        public override string DisplayName
        {
            get
            {
                string retValue = null;
                if (_categories != null &&
                    _categories.Length > 2)
                {
                    StringBuilder builder = new StringBuilder();
                    for(int i = 2; i < _categories.Length - 1; i++)
                    {
                        builder.Append(_categories[i]);
                        builder.Append("-");
                    }
                    builder.Append(_categories[_categories.Length - 1]);
                    retValue = builder.ToString();
                }
                else
                {
                    retValue = base.DisplayName;
                }
                return retValue;
            }
        }

        public override bool IsReadOnly { get { return false; } }

        public override void ResetValue(object component) { }

        public override bool CanResetValue(object component) { return false; }

        public override bool ShouldSerializeValue(object component)
        {
            return true;
        }

        public override Type ComponentType
        {
            get { return _parentPD.ComponentType; }
        }

        public override Type PropertyType { get { return _subPD.PropertyType; } }

        public override object GetValue(object component)
        {
            return _subPD.GetValue(_parentPD.GetValue(component));
        }

        public override void SetValue(object component, object value)
        {
            _subPD.SetValue(_parentPD.GetValue(component), value);
            OnValueChanged(component, EventArgs.Empty);
        }
    }
}

